#include "touchbutton.h"


TouchButton::TouchButton(QPushButton *parent) : QPushButton(parent) {
    this->setAttribute(Qt::WA_AcceptTouchEvents);
}

bool TouchButton::touchState() const
{
    return this->m_touchState;
}

bool TouchButton::event(QEvent *e)
{
    switch (e->type()) {
        //触屏按下事件开始
        case QEvent::TouchBegin: {
            this->m_touchState = true;
            emit this->pressed();
            e->accept();
            return true;
        } break;
        //触点更新
        case QEvent::TouchUpdate: {
            e->accept();
            return true;
        } break;
        //触屏事件结束
        case QEvent::TouchEnd: {
            e->accept();
            return true;
        } break;
        //鼠标被按下
        case QEvent::MouseButtonPress: {
            //若触屏事件已经触发，则忽略该次事件
            if(this->m_touchState){
                e->ignore();
            }else{
                e->accept();
            }
             return true;
        };
        //鼠标被松开
        case QEvent::MouseButtonRelease: {
            if(this->m_touchState){
                e->ignore();
            }else{
                e->accept();
            }
            //结束触屏操作标志
            this->m_touchState = false;
            return true;
        } break;
    }
    return QPushButton::event(e);
}
